package jzqb.dao.impl;

import cnki.tpi.base.DataContext;
import cnki.tpi.kbatis.sqlsession.SqlSession;
import cnki.tpi.kbatis.utils.DataSourceUtil;
import haha.life.common.SysTool;
import haha.life.normal.RecFileUtils;
import jzqb.bo.DynamicNewsBo;
import jzqb.dao.IDynamicNewsDao;
import jzqb.po.DynamicNewsPo;
import jzqb.vo.DynamicNewsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Description:
 *
 * @author BaiGe
 * @date: 2021/4/7 14:30
 * @Version 1.0
 */
@Repository
public class DynamicNewsDaoImpl implements IDynamicNewsDao {

    @Autowired
    private DataContext dataContext;

    @Override
    public List<DynamicNewsVo> queryDynamicNews_List(DynamicNewsBo dynamicNewsBo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.IDynamicNewsDao.queryDynamicNews_List";
        return sqlSession.selectList(statementId, dynamicNewsBo, false);

    }

    @Override
    public long queryDynamicNews_count(DynamicNewsBo dynamicNewsBo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.IDynamicNewsDao.queryDynamicNews_count";
        return sqlSession.getCount(statementId, dynamicNewsBo);
    }

    @Override
    public Boolean insertDynamicNews(DynamicNewsPo dynamicNewsPo) throws UnsupportedEncodingException {
        //1 写入数据到数据库
        String recStr = RecFileUtils.generateRecFileOfDynamicNews("add", dynamicNewsPo);
        String tableName = SysTool.YQTDTXW;
        boolean insetRes = dataContext.insertRecData(tableName,recStr);
        return insetRes;
    }

    @Override
    public Boolean updateDynamicNews(DynamicNewsPo dynamicNewsPo) throws UnsupportedEncodingException {
        //1 写入数据到数据库
        String recStr = RecFileUtils.generateRecFileOfDynamicNews("edit", dynamicNewsPo);
        String tableName = SysTool.YQTDTXW;
        boolean updateRes = dataContext.updateRecData("ID",tableName,recStr);
        return updateRes;
    }


    @Override
    public Boolean delDynamicNews(Integer id) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.IDynamicNewsDao.delDynamicNews";
        return sqlSession.executeDelete(statementId, id);
    }

    @Override
    public List<DynamicNewsVo> showDynamicNewsLimitFive() {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.IDynamicNewsDao.showDynamicNewsLimitFive";
        return sqlSession.selectList(statementId,null,false);
    }

    @Override
    public List<DynamicNewsVo> queryDynamicNewsOfFrontList(DynamicNewsBo dynamicNewsBo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.IDynamicNewsDao.queryDynamicNewsOfFrontList";
        return sqlSession.selectList(statementId,dynamicNewsBo,false);
    }

    @Override
    public long queryDynamicNewsOfFrontCount(DynamicNewsBo dynamicNewsBo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.IDynamicNewsDao.queryDynamicNewsOfFrontCount";
        return sqlSession.getCount(statementId,dynamicNewsBo);
    }

    @Override
    public DynamicNewsVo getDynamicNewsById(Integer dynamicNewsId) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.IDynamicNewsDao.getDynamicNewsById";
        return sqlSession.selectOne(statementId, dynamicNewsId, false);
    }
}
